import AV from "leancloud-storage";

AV.init({
  appId: process.env.VUE_APP_ID,
  appKey: process.env.VUE_APP_KEY
});

function request(fn) {
  return fn
    .then(res => {
      return res;
    })
    .catch(err => {
      console.log(err);
    });
}

function pageQuery(table, params, pageNum, pageSize) {
  for (let key in params) {
    table.equalTo(key, params[key]);
  }
  table.limit(pageSize);
  table.skip((pageNum - 1) * pageSize);

  return Promise.all([table.find(), table.count()]).then(([list, total]) => {
    return { list: list.map(item => item.toJSON()), total };
  });
}

export { AV, request, pageQuery };
